import {
    assign
} from '@/utils/assign.js'
import {
    sceneResult,zodiacConfigs,newRangeList,actNoDetail,myPurchases,actRule,myReward
} from "@/api/luck.js"

import {phoneEncrypt,keepPoint,changeLen,changeMoneyType} from '@/utils/tools'

/**
 * @auther zhonghuanting
 * 保存静动态令牌
 */
const state = {
    sceneResult: {},
    zodiacConfigs: [],
    actNoDetail: {},       //场次详情
    newRange: [],       //最新榜单
    playDtl: {},        //玩法介绍
    myPurchases:[],     //我的买入
    reward: [],         //我的奖励
}

const getters = {
    sceneResult: state => state.sceneResult,
    zodiacConfigs: state => state.zodiacConfigs,
    actNoDetail: state => state.actNoDetail,          //活动详情时间
    newRange: state => state.newRange,          //最新榜单
    playDtl: state=> state.playDtl,     //玩法介绍
    myPurchases: state=> state.myPurchases,      //我的买入
    reward: state=> state.reward        //我的奖励
}

const mutations = {
    SCENERESULT(state, data) {
        state.sceneResult =  data
    },
    ZOSIACCONFIGS(state, data) {
        state.zodiacConfigs =  data
    },
    ACT_NO_DETAIL(state,data){
        state.actNoDetail = data
    },
    NEW_RANGE(state,data){
        state.newRange = data
    },
    PLAY_DLT(state,data){
        state.playDtl = data
    },
    MY_PURCHASES(state,data){
        state.myPurchases = data
    },
    REWARD(state,data){
        state.reward = data
    }
}
const actions = {
    operSceneResult({
        commit,
        state
    }, id) {
        return new Promise((resolve, reject) => {
            sceneResult(id).then(res => {
                if (res && res.result) {
                    commit('SCENERESULT', res.result)
                    resolve(res.result)
                }else{
                    commit('SCENERESULT',"")
                    resolve("")
                }
            })
        })
    },
    operZodiacConfigs({
        commit,
        state
    }, data) {
        return new Promise((resolve, reject) => {
            zodiacConfigs().then(res => {
                if (res && res.list) {
                    commit('ZOSIACCONFIGS', res.list)
                    resolve(res.list)
                }else{
                    commit('ZOSIACCONFIGS', "")
                    resolve("")
                }
            })
        })
    },
    actNoDetailFunc({commit},sceneId){
        return new Promise((resolve,reject) => {
            actNoDetail(sceneId).then(res => {
                if(res&&res.content){
                    res.content.lotteryTimes=JSON.parse(JSON.stringify(res.content.lotteryTime))
                    // let actMonth = changeLen(String(new Date(res.content.beginTime).getMonth() + 1),2);
                    // let date = changeLen(String(new Date(res.content.beginTime).getDate()));
                    // res.content.date = actMonth + '月' + date + '日'
                    // let lotMonth = changeLen(String(new Date(res.content.lotteryTime).getMonth() + 1),2);
                    // let lotDate = changeLen(String(new Date(res.content.lotteryTime).getDate()),2);
                    if(!res.content.lotteryTime){
                       return;
                    }
                    res.content.date=res.content.date.substring(5,res.content.date.length);
                    let date=res.content.lotteryTime.split(" ");
                    let time=date[0].split("-")
                    res.content.lotteryTime = time[1] + '月' + time[2] + '日' + ' ' + date[1].substring(0,5);
                    commit('ACT_NO_DETAIL',res)
                    resolve(res)
                }else{
                    commit('ACT_NO_DETAIL',"")
                    resolve("")
                }
            }).catch(err => {
                reject(err)
            })
        })
    },
    newRangeFunc({commit},params){
        return new Promise((resolve,reject) => {
            newRangeList(params).then(res => {
                if(res&&res.ranking && res.ranking.length > 0){
                    commit('NEW_RANGE',res.ranking)
                }else{
                    commit('NEW_RANGE',"")
                }
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    },
    playDtl({commit},params){
        return new Promise((resolve,reject) => {
            actRule(params).then(res => {
                if(res && res.rule){
                    commit('PLAY_DLT',res)
                }else{
                    commit('PLAY_DLT',"")
                }
                resolve(res)
            }).catch(err=> {
                reject(err)
            })
        })
    },
    myPurchasesFunc({commit},params){
        return new Promise((resolve,reject)=>{
            myPurchases(params).then(res=> {
                if(res && res.buys.length > 0){
                    res.buys.forEach(item => {
                        // let month = changeLen(String(new Date(item.ctime).getMonth() + 1),2);
                        // let date = changeLen(String(new Date(item.ctime).getDate()),2);
                        item.ctime =item.ctime.substring(5,item.ctime.length)
                    })
                    commit('MY_PURCHASES',res.buys)
                    resolve(res.buys)
                }else{
                    commit('MY_PURCHASES',"")
                    resolve("")
                }
            }).catch(err=> {
                reject(err)
            })
        })
    },
    rewardFunc({commit},params){
        return new Promise((resolve,reject) => {
            myReward(params).then(res => {
                if(res && res.awards){
                    commit('REWARD',res.awards)
                    resolve(res.awards)
                }else{
                    commit('REWARD',"")
                    resolve("")
                }
            }).catch(err => {
                reject(err)
            })
        })
    }
}

export default {
    state,
    getters,
    mutations,
    actions
}